package com.platform.framework.controller.rights;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.platform.framework.controller.rights.SendCode.CheckSumBuilder;

public class CheckCode {
	public static String checkCode(String mobile, String code) throws Exception {
		String result="0";
		try {
			CloseableHttpClient httpclient = HttpClients.createDefault();
	        String url = "https://api.netease.im/sms/verifycode.action";
	        HttpPost httpPost = new HttpPost(url);
	        String appKey = "ea1b2443ccbf15ec52db4c93ef25bab8";
	        String appSecret = "f80567c7133a";
	        String nonce =  "123456";
	        String curTime = String.valueOf((new Date()).getTime() / 1000L);
	        String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce ,curTime);//参考 计算CheckSum的java代码

	        // 设置请求的header
	        httpPost.addHeader("AppKey", appKey);
	        httpPost.addHeader("Nonce", nonce);
	        httpPost.addHeader("CurTime", curTime);
	        httpPost.addHeader("CheckSum", checkSum);
	        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

	        // 设置请求的参数
	        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
	        nvps.add(new BasicNameValuePair("mobile", mobile));
	        nvps.add(new BasicNameValuePair("code", code));
	        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));

	        // 执行请求
	        HttpResponse response = httpclient.execute(httpPost);
	        String data = EntityUtils.toString(response.getEntity(), "utf-8");
	        JSONObject obj = JSON.parseObject(data);
	        String num =obj.get("code").toString();
	        if (num.equals("200")){
	        	result= "1";
	        	System.out.println("验证成功！（"+code+"）");
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	    }

	/**
	 * @param args
	 * @throws java.io.IOException 
	 *//*
	public static void main(String[] args) throws Exception {
		String result = CheckCode.checkCode("13807031023", "518683");
		System.out.println(result);
	}*/
}